package com.smartmail.receiving.imap;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.smartmail.accounts.MailAccount;
import com.smartmail.mailing.MailMessage;
import com.smartmail.receiving.AbstractMessageHandler;
import com.smartmail.receiving.InternalStorageUtility;
import com.smartmail.receiving.IncomingMailDBManager;
import com.smartmail.receiving.Receiving;
import com.smartmail.utility.MessageUtility;

/**
 * Implementation of AbstractMessageHandler class.
 * This class is  responsible for handling new messages from IMAP mailbox.
 * 
 * @author Tomek
 *
 */
public class IMAPMessageHandler extends AbstractMessageHandler {
	
	/**
	 * Public construtor.
	 * 
	 * @param context - application context
	 * @param mailAccount - Messages from this mailbox account will be handled
	 */
	public IMAPMessageHandler(Context context, MailAccount mailAccount){
		
		super(context,mailAccount);
	}
	
	/**
	 * This method handles one MimeMessage object (mail message) from the Inbox.
	 * First, it is checked whether this message is already stored in device.
	 * If this is known message nothing is to do.
	 * Secondly, message headers are saved in incomimg mails database, and 
	 * text part of message (cache) is saved in device's internal memory.
	 * If message is 'new' NEW_MESSAGE_ARRIVED_INTENT is sent.
	 * 
	 * @param mimeMessage - message object which will be handled
	 * @param messageUID -  UID field of message
	 * @return - one of the four fields :
	 * - HANDLE_ERROR 					- for failure purposes
	 * - HANDLE_NEW_MESSAGE 				- when mimeMessage is new, and info about is saved in device
	 * - HANDLE_NEW_MESSAGE_DOWNLOADED	- when mimeMessage is new, and is completely downloaded to device
	 * - HANDLE_KNOWN_MESSAGE 			- when mimeMessage is already stored in device
	 */
	public int handle(MimeMessage mimeMessage, String messageUID){
		
		try {
			
			IncomingMailDBManager dbManager = Receiving.getDBManager(_mailAccount.getAccountID());
			
			/**
			 * check if this message is already in device
			 * 
			 * this is completely new message
			 * save its headers in database and check if
			 * it could be downloaded
			 */
			if (!dbManager.isAlreadyInDB(mimeMessage)) {

				/**
				 * if writing to database succeed send intent
				 */
				if(dbManager.writeMessageHeaders(mimeMessage,messageUID,_mailAccount)){
					
					dbManager.setState(MailMessage._MESSAGE_DOWNLOADED_NOT_READ, mimeMessage);
					
					String path = InternalStorageUtility.IMAP.writeCacheOnSDCard(mimeMessage);
					dbManager.setPath(path, mimeMessage);

					// send intent to refresh AccountMsgActivity
					Intent intent = new Intent(Receiving.NEW_MESSAGE_ARRIVED_INTENT);
					intent.putExtra(Receiving.MAIL_INFO_ACCOUNT_ID, _mailAccount.getAccountID());
					intent.putExtra(Receiving.MAIL_INFO_HASH, MessageUtility.getHash(mimeMessage));
					_context.sendBroadcast(intent);
					
					Log.d(_logTag, "IMAPMessageHandler : New Message : " + mimeMessage.getSubject() + " - " +
					_mailAccount.getMailAddress());
				}	
				else
					return HANDLE_ERROR;
				
				return HANDLE_NEW_MESSAGE;
			}
			else
				return HANDLE_KNOWN_MESSAGE;

		} catch (MessagingException e) {
			
			Log.d(_logTag, "IMAPMessageHandler : error while serving mimemessage");
			return HANDLE_ERROR;
		}
		
	}

	/**
	 * The method used to download the complete message from the server.
	 * This method is only used for the POP3 protocol.
	 * In IMAP case, method always returns HANDLE_ERROR
	 * 
	 * @param mimeMessage - message object wanted to be downloaded
	 * @return - HANDLE_ERROR
	 */
	public int download(MimeMessage mimeMessage) {
		
		Log.d(_logTag,
				"IMAPMessageHandler : in IMAP protocol this is unsupported - "+ _mailAccount.getMailAddress());
		return HANDLE_ERROR;
	}

}
